//
//  ACMemoryStorageViewController.m
//  LFUCache_Example
//
//  Created by autohome on 2021/8/16.
//  Copyright © 2021 caixiang305621856. All rights reserved.
//

#import "ACMemoryStorageViewController.h"
#import "ACMemoryCache.h"

@interface ACMemoryStorageViewController ()

@property (strong, nonatomic) ACMemoryCache *cache;

@end

@implementation ACMemoryStorageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self beginCache];
}

- (void)beginCache {
    for (int i = 0; i<100; i++) {
        NSString *obj = [NSString stringWithFormat:@"object--%d",i];
        [self.cache setObject:obj forKey:@(i)];
    }
}

- (ACMemoryCache *)cache {
    if (!_cache) {
        _cache = [[ACMemoryCache alloc] initWithName:@"MS"];
        _cache.countLimit = 5;
        _cache.costLimit = 8000;
        _cache.didEnterBackgroundBlock = ^(ACMemoryCache * _Nonnull cache) {
            for (int i = 0; i < 10; i ++) {
                NSString *s =   [cache objectForKey:@(i)];
                NSLog(@"%@ didEnterBackgroundBlock ",s);
            }
        };
    }
    return _cache;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    for (int i = 0; i < 100; i ++) {
        NSString *s =   [self.cache objectForKey:@(i)];
        NSLog(@"%@ objectForKey ",s);
    }
}

- (void)dealloc {
    NSLog(@"%s",__func__);
}

@end
